package Package3;

public class function {
    public static void main(String[] args) {
        int[] arr1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        printArray(arr1);
        System.out.println("最大值是：" + getMaxArr(arr1));

        int[] arr2 = copyOfRange(arr1, 2, 7);
        printArray(arr2);
    }

    //打印输出数组
    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i != arr.length - 1) {
                System.out.print(arr[i] + ", ");
            } else {
                System.out.print(arr[i]);
            }
        }
        System.out.println("]");
    }

    //求数组的最大值
    public static int getMaxArr(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        return max;
    }

    //复制数组
    public static int[] copyOfRange(int[] arr, int from, int to) {
        int[] tempArr = new int[to - from];
        for (int i = from, j = 0; i < to; i++, j++) {
            tempArr[j] = arr[i];
        }
        return tempArr;
    }


}
